window.addEventListener('load', function () {
  //  今日头条
  joke()
  function getData() {
    let main = document.querySelector('#main')
    let Cnba = document.querySelector('.NBA')
    Cnba.innerHTML = ''
    main.innerHTML = ''
    main.style.className = ''
    axios({
      method: 'GET',
      url: "https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/topnews"
    })
      .then(res => {
        let data = res.data.result.data
        let arr = []
        data.forEach(item => {
          arr.push(`
   <div class="today">
      <img
        src="${item.thumbnail_pic_s}"
        alt="">
      <div class="today-right">
        <a href="${item.url}">${item.title}</a>
        <div class="today-right-bottom">
          <div>
            <span>${item.category}</span>
            <span>${item.author_name}</span>
          </div>
          <div class="today-time">${item.date}</div>
        </div>
      </div>
    </div>
          `)
        })
        main.innerHTML = arr.join('')
      })
  }
  // 笑话
  function joke() {
    let main = document.querySelector('#main')
    let Cnba = document.querySelector('.NBA')
    Cnba.innerHTML = ''
    main.innerHTML = ''
    main.classList.add('joke')
    let xhr = new XMLHttpRequest()
    xhr.open('GET', 'https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/joke')
    xhr.send()
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        let jokejson = JSON.parse(xhr.responseText)
        jokejson.result.data.forEach(item => {
          main.innerHTML += `${template('joke', item)}`
        })
      }
    }
  }

  function NBAFn() {
    let main = document.querySelector('#main')
    let Cnba = document.querySelector('.NBA')
    Cnba.innerHTML = `
    <div class="NBAtitlr">美国男子职业篮球联赛</div>
    <div id="loop">

    </div>
    `
    main.innerHTML = ''
    main.classList.remove('joke')
    var xhr = new XMLHttpRequest()
    xhr.open('GET', 'https://mobile-ms.uat.homecreditcfc.cn/mock/61727df60155e30027287be1/example/nba')
    xhr.send()
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        // 获取服务器响应的数据
        let ccObj = xhr.responseText;
        let obj = JSON.parse(ccObj)
        let loop = document.querySelector('#loop')
        let str = ''
        let newStr = ''
        let obj1 = obj.result.matchs;
        console.log(obj1);
        for (let i = 0; i < obj1.length; i++) {
          str = `<div div class="NBAdate" >
           <div class="NBATime">${obj1[i].date}${obj1[i].week}</div>
           </div> 
         </div>`
          newStr = ''
          for (let j = 0; j < obj1[i].list.length; j++) {
            let num = obj.result.matchs[i].list[j]
            newStr += `<div class="NBAdate1">${num.team1}vs${num.team2}&nbsp; <div class="NBAdate1_1">(${num.status_text})&nbsp;${num.time_start}</div>
            </div>
            <div class="NBAdate2">${num.team1_score}&nbsp;--&nbsp;${num.team2_score}</div>`
          }
          loop.innerHTML += str + newStr
        }
      }
    }
  }

  // 绑定点击事件
  let jokes = document.querySelector('#jokes')
  let today = document.querySelector('#today')
  let NBA = document.querySelector('#NBA')
  jokes.addEventListener('click', joke)
  today.addEventListener('click', getData)
  NBA.addEventListener('click', NBAFn)
})




